package com.example.OnlineStorage.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class GlobalCorsConfig implements WebMvcConfigurer {
    private final String[] ALLOW_ORIGIN ={
            "http://localhost",
            "http://127.0.0.1",
    };
    @Bean
    public CorsFilter myCorsFilter() {
        //创建“跨域配置器”：
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);//允许发送Cookie信息
        config.addAllowedOriginPattern("*");//允许跨域请求的域名，*表示所有
        config.addAllowedMethod("*");//允许跨域的请求方法，*表示所有
        config.addAllowedHeader("*");//允许跨域的请求头，*表示所有
        config.addExposedHeader("Token");
        config.addExposedHeader("");
        config.addExposedHeader("");
        //注册“跨域配置器”：
        UrlBasedCorsConfigurationSource configSource =
                new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**", config);//按上述配置来注册，“/**”表示所有路径
        //按上述配置来创建过滤器，然后返回它：
        return new CorsFilter(configSource);
    }
}
